// pages/my/my.js
const to = require("../../utils/jump")
import { createUser, getUserInfo, phone } from '../../utils/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    img: {},
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getimg()
    // console.log(this.data.user)
    this.getUserInfo()
  },
  //获取图片资源
  getimg() {
    this.setData({
      img: {
        bg: wx.getStorageSync('bg0'),
        dingdan: wx.getStorageSync('my0'),
        expleft: wx.getStorageSync('my1'),
        expright: wx.getStorageSync('my2'),
        hunjing: wx.getStorageSync('my3'),
        kuang: wx.getStorageSync('my4'),
        level: wx.getStorageSync('my5'),
        touxiang: wx.getStorageSync('my6'),
        youhuijuan: wx.getStorageSync('my7'),
        yue: wx.getStorageSync('my8'),
      }
    })
  },
  // get获取用户参数
  async getUserInfo() {
    let user = wx.getStorageSync('user')
    this.setData({
      user
    })
    const getUser = await getUserInfo({ nickname: user.nickname, head_url: user.avatarUrl })
    console.log(getUser)
    this.setData({
      user: getUser
    })
  },
  //余额
  toYue() {
    to('/wanfa/pages/yue/yue')
  },
  // 魂晶明细
  toHunjing() {
    to('/wanfa/pages/hunjingmingxi/hunjingmingxi')
  },
  //积分明细
  toJifen() {
    to('/wanfa/pages/jifenmingxi/jifenmingxi')
  },
  //获取手机号
  getPhoneNumber(e) {
    // this.toLogin()
    console.log(e.detail.cloudID)
    let cloudID = e.detail.cloudID
    phone({ cloudID })
    // console.log(phone)
  },
  toLogin() {
    // 获取用户昵称
    wx.getUserProfile({
      desc: '用于授权完整用户信息',
      success: (res) => {
        console.log(res)
        wx.setStorageSync('user', res.userInfo)
        createUser({ nickname: res.userInfo.nickName, head_url: res.userInfo.avatarUrl })
        this.getUserInfo()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})